package com.viking.authentication.gui;

import com.viking.gui.catchspermmaininterface.CatchSpermMainInterface;
import com.viking.util.Base64Img;
import com.viking.util.GetImg;
import com.viking.util.Post;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.sql.SQLException;

import static javax.swing.SpringLayout.*;

public class LoginWindow implements Runnable
{

    @Override
    public void run()
    {
        GetImg getImg =new GetImg();

        try {
            Base64Img.GenerateImage(getImg.GetImg(), "/11.jpg");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        JFrame jf = new JFrame("登录窗口");
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setSize(500, 330);
        JPanel jp = new JPanel();
        jf.setContentPane(jp);
        SpringLayout springLayout = new SpringLayout();
        jp.setLayout(springLayout);




        //显示账号
        JLabel account_text = new JLabel("账号：");
        jp.add(account_text);
        account_text.setForeground(Color.white);
        springLayout.putConstraint(NORTH, account_text, 20, NORTH, jp);  //标签1北侧——>容器北侧
        springLayout.putConstraint(WEST, account_text, 130, WEST, jp);    //标签1西侧——>容器西侧 10

        //文本框组件的位置约束
        JTextField account = new JTextField();
        jp.add(account);
        account.setPreferredSize(new Dimension (200,30));
        springLayout.putConstraint(NORTH, account, 20, NORTH, jp);//文本框北侧——>容器北侧
        springLayout.putConstraint(WEST, account, 0, EAST, account_text);//文本框西侧——>主题标签东侧
        //springLayout.putConstraint(EAST, account, -50, EAST, jp);// 文本框东侧——>容器东侧

        //标签组件2的位置约束
        JLabel password_text= new JLabel("密码: ");
        jp.add(password_text);
        password_text.setForeground(Color.white);
        springLayout.putConstraint(NORTH, password_text, 20, SOUTH, account);// 内容标签北侧(参照文本框是因为文本框高)——>主题文本框南侧
        springLayout.putConstraint(WEST, password_text, 130, WEST, jp);// 内容标签西侧——>容器西侧

        JPasswordField Password = new JPasswordField();
        jp.add(Password);
        Password.setPreferredSize(new Dimension (200,30));
        springLayout.putConstraint(NORTH, Password, 25, SOUTH, account);// 滚动面板北侧——>文本框南侧
        springLayout.putConstraint(WEST, Password, 10, EAST, password_text);// 滚动面板西侧——>内容标签东侧
        //springLayout.putConstraint(EAST, Password, -50, EAST, jp);// 滚动面板东侧——>容器东侧

        JButton Btn_confirm = new JButton("确认");
        Btn_confirm.setBorderPainted(false);
        Btn_confirm.setBackground(Color.getHSBColor(67,100,100));

        jp.add(Btn_confirm);
        Btn_confirm.setPreferredSize (new Dimension (60,25));
        //Btn_confirm.setIcon(new ImageIcon(getClass().getResource("/button.png"))); ;
        springLayout.putConstraint(NORTH, Btn_confirm, 90, NORTH,Password);// 按钮2南侧——>容器南侧
        springLayout.putConstraint(WEST, Btn_confirm, 0, WEST,Password);// 按钮2东侧——>密码框东侧

        JButton Btn_cancel = new JButton("取消");
        jp.add(Btn_cancel);
        Btn_cancel.setBackground(Color.getHSBColor(67,100,100));

        springLayout.putConstraint(NORTH,Btn_cancel,0,NORTH,Btn_confirm);
        springLayout.putConstraint(EAST,Btn_cancel,70,EAST, Btn_confirm);

        JButton Btn_Forget = new JButton("忘记密码");
        jp.add(Btn_Forget);
        Btn_Forget.setBackground(Color.getHSBColor(67,100,100));
        springLayout.putConstraint(NORTH,Btn_Forget,50,NORTH,Btn_confirm);
        springLayout.putConstraint(EAST,Btn_Forget,70,EAST, Btn_confirm);

        JButton Btn_Logon = new JButton("注册账号");
        jp.add(Btn_Logon);
        Btn_Logon.setBackground(Color.getHSBColor(67,100,100));
        springLayout.putConstraint(NORTH,Btn_Logon,0,NORTH,Btn_Forget);
        springLayout.putConstraint(EAST,Btn_Logon,-100,EAST, Btn_Forget);

        //显示验证码
        JLabel code = new JLabel();
        ImageIcon img = new ImageIcon("/11.jpg");
        code.setIcon((Icon)img);
        code.setBounds(450, 290,150, 50);
        springLayout.putConstraint(NORTH,code,-50, NORTH,Btn_confirm);
        springLayout.putConstraint(EAST,code,160, EAST,Btn_confirm);
        jp.add(code);

        JLabel Captcha_text = new JLabel("验证码：");
        jp.add(Captcha_text);
        Captcha_text.setForeground(Color.white);
        springLayout.putConstraint(NORTH,Captcha_text,0,NORTH,code);
        springLayout.putConstraint(EAST,Captcha_text,-220,EAST, code);

        JTextField Captcha = new JTextField();
        jp.add(Captcha);
        Captcha.setPreferredSize(new Dimension (80,30));
        springLayout.putConstraint(NORTH,Captcha,0,NORTH,Captcha_text);
        springLayout.putConstraint(EAST,Captcha,80,EAST, Captcha_text);
        //确认按钮
        Btn_confirm.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {

                    System.out.println(Password.getPassword());
                try {
                        Post post =new Post(account.getText(),Password.getPassword(),Captcha.getText(),getImg.getCheckKey());
                    System.out.println(post.inSure);
                        if(post.inSure)
                        {
                            CatchSpermMainInterface catchSperm = new CatchSpermMainInterface();
                            catchSperm.setVisible(true);
                            jf.setVisible(false);
                            jf.dispose();
                        }


                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null,"密码或验证码错误");
                    jf.dispose();
                    //jf.setVisible(false);
                    Thread thread1 = new Thread(new LoginWindow());
                    thread1.start();
                }



            }
        });
        //忘记密码按钮
        Btn_Forget.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                LoginWindowForgetPassword M2 =new LoginWindowForgetPassword();
                jf.setVisible(false);
            }
        });
        //注册账号按钮
        Btn_Logon.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                LoginWindowLogon M3 = new LoginWindowLogon();
                jf.dispose();
                //jf.setVisible(false);
            }
        });
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setVisible(true);  // 显示窗口
        jf.setResizable(false);

        // 设置背景
        JLabel lblBackground = new JLabel(); // 创建一个标签组件对象
        URL resource = this.getClass().getResource("R-C.gif"); // 获取背景图片路径
        /** 没有背景资源，暂时注释掉*/
        /*
        ImageIcon icon = new ImageIcon(resource); // 创建背景图片对象
        lblBackground.setIcon(icon); // 设置标签组件要显示的图标
        lblBackground.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight()); // 设置组件的显示位置及大小
        jp.add(lblBackground);
*/



    }


}
